package com.cd.music.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author yxl 余秀良
 * @version 1.0
 * @description: 验证码记录表
 * @date 2018年03月01日  下午3:21
 * @address: 四川成都
 */
@Entity
@Table
public class AuthCodeRecord implements Serializable {
    private static final long serialVersionUID = 4505765449753147930L;
    /**
     * 主键id
     */
    @Id
    @Column(nullable = false, length = 32, unique = true, name = "id")
    private String id;
    /**
     * 手机号码
     */
    @Column(name = "phone", length = 11, nullable = false)
    @Pattern(regexp = "^(13[0-9]|15[0-3,5-9]|17[0135678]|18[0-9]|19[89])\\d{8}$")
    private String phone;
    /**
     * 验证码
     */
    @Column(length = 20, name = "verificationCode", nullable = false, columnDefinition = "nvarchar(20)")
    private String verificationCode;
    /**
     * 发送时间
     */
    @Column(name = "sendTime", columnDefinition = "datetime default now()")
    private Date sendTime;
    /**
     * 过期时间
     */
    @Column(name = "expireTime", columnDefinition = "datetime")
    private Date expireTime;

    /**
     * 有效期,(单位分钟)
     */
    @Column(name = "effectiveTime", columnDefinition = "tinyint(1) default 5")
    private int effectiveTime = 5;
    /**
     * 是否过期　默认为未过期 true表示已过期，false表示未过期
     */
    private boolean isExpire = false;
    /**
     * 验证码的状态,true表示已使用，false表示未使用
     */
    private boolean isUse = false;

    /**
     * 备注
     */
    @Column(name = "remark", length = 200)
    private String remark;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getVerificationCode() {
        return verificationCode;
    }

    public void setVerificationCode(String verificationCode) {
        this.verificationCode = verificationCode;
    }

    public Date getSendTime() {
        Calendar now = Calendar.getInstance();
        now.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return sendTime = now.getTime();
    }

    public Date getExpireTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(getSendTime());
        calendar.add(Calendar.MINUTE, getEffectiveTime());
        return expireTime = calendar.getTime();
    }

    public int getEffectiveTime() {
        return effectiveTime;
    }

    public void setEffectiveTime(int effectiveTime) {
        this.effectiveTime = effectiveTime;
    }

    public boolean getIsExpire() {
        long expireTime = getExpireTime().getTime();
        long nowTime = System.currentTimeMillis();
        return isExpire = nowTime >= expireTime;
    }

    public boolean getIsUse() {
        return isUse;
    }

    public void setIsUse(boolean use) {
        isUse = use;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

}
